#include "wds_ui_popupanimator.h"
#include "wds_ui_setting.h"
#include "wds_ui_pausemenu.h"
#include "wds_ui_msgbox.h"
#include "wds_ui_gameover.h"
#include "wds_ui_animator.h"
#include "wds_ui_yesno.h"

#include "audio/audio.h"
#include "ui/ui_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "gameflow/ui_state.h"

#include "util/common_macros.h"

using namespace gameflow;

WDSUIPopupAnimator::WDSUIPopupAnimator(const std::string& Name,const std::string& CloseButton)
{
	m_pPopup = UIPopupManager::Instance().AddPopup(Name,CloseButton);
}

WDSUIPopupAnimator::WDSUIPopupAnimator(gameflow::UIPopup* pPopup) : m_pAnimateWidget(NULL)
{
	UIPopupManager::Instance().AddPopup(pPopup);
	m_pPopup = pPopup;
}

WDSUIPopupAnimator::WDSUIPopupAnimator() :	m_pAnimateWidget(NULL)											
{

}

WDSUIPopupAnimatorMgr&     WDSUIPopupAnimatorMgr::Instance()
{
	static WDSUIPopupAnimatorMgr	Inst;
	return Inst;
}

WDSUIPopupAnimatorMgr::WDSUIPopupAnimatorMgr()
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
		m_pPopups[i] = NULL;
	
}

void		WDSUIPopupAnimatorMgr::AddPopupAnimator(POPUP_NAMES Name,WDSUIPopupAnimator* pPopupAnimator )
{
	if(pPopupAnimator)
		m_pPopups[Name] = pPopupAnimator;
}

WDSUIPopupAnimatorMgr::~WDSUIPopupAnimatorMgr()
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		UTIL_SAFE_DELETE(m_pPopups[i]);
	}
}


void	WDSUIPopupAnimatorMgr::Tick(float dt)
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
			m_pPopups[i]->Tick(dt);
	}
}

void	WDSUIPopupAnimatorMgr::OnMouseButtonDown(const math::Vec2& Pos)
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
			m_pPopups[i]->OnMouseButtonDown(Pos);
	}
}

void	WDSUIPopupAnimator::Hide()
{
	AUDIO().PlayEvent("popup_close");
}

WDSUIPopupAnimator*	WDSUIPopupAnimatorMgr::Show(POPUP_NAMES Name)
{
	

	if(m_pPopups[Name])
	{
		m_pPopups[Name]->Show();
		return m_pPopups[Name];
	}
	else
	{
		

		if(GetPopup(Name))
		{
			m_pPopups[Name]->Show();
		}
	}	
	return m_pPopups[Name];

}

WDSUIPopupAnimator*		WDSUIPopupAnimatorMgr::GetPopup(POPUP_NAMES Name)
{
	if(m_pPopups[Name])
		return m_pPopups[Name];

	// lazy loading
	switch(Name)
	{	
	case POPUP_NAMES_SETTING:
		{
			gameflow::UIPopup *pPopup = gameflow::UIPopupManager::Instance().GetPopup("ui/settings.ui");
			new WDSUISetting(pPopup);
		}
		break;
	case POPUP_NAMES_PAUSEMENU:
		new WDSUIPauseMenu();
		break;
	case POPUP_NAMES_MSGBOX:
		new WDSUIMsgBox();
		break;
	case POPUP_NAMES_GAMEOVER:
		new WDSUIGameOver();
		break;
	case POPUP_NAMES_ACHIEVEMENT:
		
		break;
	case POPUP_NAMES_YESNO:
		{
			gameflow::UIPopup* pPopup = gameflow::UIPopupManager::Instance().GetPopup("ui/yesno.ui");
			new WDSUIYesNo(pPopup);
		}
		break;
	case POPUP_NAMES_ACHIEVEMENTGAIN:

		
		break;
	}

	return m_pPopups[Name];
}

void	WDSUIPopupAnimator::Show()
{
	AUDIO().PlayEvent("popup_open");

	if(m_pPopup)
		m_pPopup->Show(NULL);

	
	switch(m_Type)
	{	
	case ANIMATION_MSGPOPUP:
		WDSUIAnimator::Instance().Add(m_pAnimateWidget,WDSAnimatedUI::ANIMATE_STYLE_POPPINGUP,0, NULL);
		break;
	case ANIMATION_SLIDEFROMRIGHT:
		if(m_pAnimateWidget)
			m_pAnimateWidget->SetPosition(math::Vec2(1.5f,0.5f));
		WDSUIAnimator::Instance().Add(m_pAnimateWidget,WDSAnimatedUI::ANIMATE_STYLE_CENTERSCREEN,0, NULL);
		break;
	case ANIMATION_SLIDEFROMTOP:
		if(m_pAnimateWidget)
			m_pAnimateWidget->SetPosition(math::Vec2(0.5f,-1.5f));
		WDSUIAnimator::Instance().Add(m_pAnimateWidget,WDSAnimatedUI::ANIMATE_STYLE_CENTERSCREEN,0, NULL);
		break;
	default:
		break;
	}

	
	
}

void	WDSUIPopupAnimator::Tick(float dt)
{
	

	switch(m_Type)
	{
	case ANIMATION_MSGPOPUP:

		break;
	case ANIMATION_SLIDEFROMRIGHT:

		break;
	case ANIMATION_SLIDEFROMTOP:


		break;
	default:
		break;
	}



}

bool	WDSUIPopupAnimatorMgr::KeyPressed(input::Keyboard::KEY* pKey)
{
	for(int i = 0; i < POPUP_NAMES_C; i++)
	{
		if(m_pPopups[i])
			if(m_pPopups[i]->KeyPressed(pKey))
				return true;
	}
	return false;
}
